package com.itedge.solutionmanager.web.controller.process.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itedge.solutionmanager.constants.SolutionManagerConstants;
import com.itedge.solutionmanager.domain.impl.Customer;
import com.itedge.solutionmanager.domain.impl.Solution;
import com.itedge.solutionmanager.domain.dto.SolutionDto;
import com.itedge.solutionmanager.enumeration.SolutionPhase;
import com.itedge.solutionmanager.service.domain.ISolutionService;
import com.itedge.infrastructure.service.process.IProcessService;
import com.itedge.infrastructure.util.WebUtil;
import com.itedge.infrastructure.web.controller.process.impl.AbstractProcessEntityController;

@RequestMapping("/solutions")
@Controller
public class SolutionController extends AbstractProcessEntityController<Solution> {

    private IProcessService<Solution> solutionProcessService;	  
    private ISolutionService solutionService;   

    @Autowired
    protected SolutionController(ISolutionService solutionService, 
    		IProcessService<Solution> solutionProcessService, MessageSource messageSource) {
		super(solutionService, solutionProcessService, messageSource);
		this.solutionProcessService = solutionProcessService;
		this.solutionService = solutionService;
	}

	@Override
	public String getModelKeyForEntitiesList() {
		return "solutions";
	}

	@Override
	protected String getModelKeyForEntity() {
		return "solution";
	}
	
	@Override
	public String getListView() {
		return "solutions/list";
	}

	@Override
	protected String getShowView() {
		return "solutions/show";
	}
	
	@Override
	public int getDefaultPageSizeForListView() {
		return SolutionManagerConstants.DEFAULT_SIZE_PER_PAGE;
	}

	@Override
	protected void addModelAttributesForShow(Model model) {
		Solution solution = (Solution)model.asMap().get(getModelKeyForEntity());
		model.addAttribute("linkedCustomer", solution.getCustomer() != null ? solution.getCustomer() : new Customer());	
	}
    
	@RequestMapping(method = RequestMethod.POST)
    public String create(@Valid Solution solution, BindingResult result, Model model, HttpServletRequest request) {
        if (result.hasErrors()) {
            model.addAttribute("solution", solution);
            return "solutions/create";
        }
        solution.setPhase(SolutionPhase.CONTACT);
    	solution.setContactDate(new Date());
    	solutionProcessService.createEntityProcess(solution);
        return "redirect:/solutions/" + WebUtil.encodeUrlPathSegment(solution.getId().toString(), request);
    }

	@RequestMapping(params = "form", method = RequestMethod.GET)
    public String createForm(Model model) {
        model.addAttribute("solution", new Solution());
        return "solutions/create";
    }
	
	@RequestMapping(params = "searchForm", method = RequestMethod.GET)
    public String searchForm(Model model) {
        return "solutions/search";
    }
	
	/*
	 * Method used for AJAX search requests on solutions, returning data in JSON format
	 */
	@RequestMapping(params = "searchForm", method = RequestMethod.POST, headers = "Accept=application/json")
    public @ResponseBody List<SolutionDto> searchAsyncForSolutions(@RequestParam(value = "searchName") String searchName,
    		@RequestParam(value = "maxResults", required = false) Integer maxResults, HttpServletRequest request) {		
		Locale currentLocale = request.getLocale();	
		List<SolutionDto> solutionDtoList = new ArrayList<SolutionDto>();
        Solution searchSolution = new Solution();
        searchSolution.setInitialInfo(searchName);
        searchSolution.setName(searchName);
        Iterator<Solution> it = solutionService.findEntitiesByCriteria(searchSolution, maxResults).iterator();
        while(it.hasNext()) {
        	solutionDtoList.add(new SolutionDto(it.next(), messageSource, currentLocale));
        }
		return solutionDtoList;
    }

	@ModelAttribute("solutionphases")
    public Collection<SolutionPhase> populateSolutionPhases() {
        return Arrays.asList(SolutionPhase.class.getEnumConstants());
    }
	
}
